package com.kaiyiweitong.tongxing.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.MainActivity;
import com.kaiyiweitong.tongxing.activity.search.SearchActivity;
import com.kaiyiweitong.tongxing.adapter.HistorySearchAdapter;
import com.kaiyiweitong.tongxing.adapter.SearchTagAdapter;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by Administrator on 2017/12/15 0015.
 */

public class SearchFragment extends Fragment implements TagFlowLayout.OnTagClickListener {
    @Bind(R.id.tfl_hot_search)
    TagFlowLayout mTflHotSearch;
    @Bind(R.id.rv_history_search)
    RecyclerView mRvHistorySearch;

    private MainActivity mActivity;
    private List<String> mHotSearchList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_search, null);
        ButterKnife.bind(this, view);
        mActivity = (MainActivity) getActivity();
        init();
        return view;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    private void init() {
        mHotSearchList = new ArrayList<>();
        mHotSearchList.add("内资公司注册");
        mHotSearchList.add("代理记账");
        SearchTagAdapter adapter = new SearchTagAdapter(mHotSearchList);
        mTflHotSearch.setAdapter(adapter);
        mTflHotSearch.setOnTagClickListener(this);

        HistorySearchAdapter historySearchAdapter = new HistorySearchAdapter(mActivity, mListener);
        mRvHistorySearch.setLayoutManager(new GridLayoutManager(mActivity, 1, GridLayout.VERTICAL, false));
        mRvHistorySearch.setAdapter(historySearchAdapter);

    }

    @OnClick(R.id.ll_search)
    public void onViewClicked() {
        go2Page(null);
    }

    @Override
    public boolean onTagClick(View view, int position, FlowLayout parent) {
        go2Page(mHotSearchList.get(position));
        return true;
    }

    private HistorySearchAdapter.OnClickListener mListener = new HistorySearchAdapter.OnClickListener() {
        @Override
        public void onClick(int position) {
            go2Page("公司注册");
        }

        @Override
        public void onDelete(int position) {
            CommonUtil.toast(getActivity(), "删除第" + position + "个");
        }
    };

    private void go2Page(String searchText) {
        Intent intent = new Intent(mActivity, SearchActivity.class);
        intent.putExtra(SearchActivity.SEARCH_TEXT, searchText);
        startActivity(intent);
    }

}
